This page describes a way that you can enhance Dynamics CRM Workflow Processes with the Open Source ClosedXML project to add the power of Excel formulas to workflows.

Note: The information on this page applies to Dynamics CRM 2013 Online and On-premises.

One of the features provided with ClosedXML is the ability to evaluate Excel formulas. CRM can execute these formulas within workflow activityplug-ins. Once you've deployed the workflow activity, individuals in your organization who can create workflows and write Excel formulas can significantly enhance the capabilities of Dynamics CRM without custom programming. For example, it's possible to combine the use of the Excel WEEKDAY function to add logic to a workflow based on the day of the week.

Here's a small sample of the hundreds of possible formulas you'll be able to utilize within CRM workflows:

NETWORKDAYS(Date1, Date2)
Get the number of work days between two DateTime field values.
NETWORKDAYS(Date1, Date2) + AddDays
Same as above, but adds a specified number of days to the work days calculation.
Date1 + AddDays
Get the date that is n days before or after the specified date.
EOMONTH(Date1,1)
Get the date that is the last day of next month.
INT((Date1 - Date2) * 24)
Get the number of hours between two date/time values.
SUBSTITUTE(InputString, " ", "")
Search and replace within a string.

The ClosedXML authors maintain the list of available Excel formula functions. As of the last update of the page, there are 124 functions supported.

Required Components

There's a need to build a CRM workflow activity that you and your end-users will use within CRM workflows. The applications and components needed to build the workflow activity plug-in are listed below.
  • Microsoft Dynamics CRM 2013 Online or On-premises
  • Microsoft Dynamics CRM 2013 SDK (CRM assemblies, plug-in registration tool)
  • Microsoft Visual Studio 2013
  • ClosedXML source code (You'll compile this to ClosedXML.dll. There is one line of code you need to change to allow the code to work for CRM Online. Search for "Author =" in XLWorkbook.cs and hard-code a value for the Author property.)
  • OpenXML SDK (DocumentFormat.OpenXml.dll; Included with ClosedXML source code)
  • ILMerge

Technical Design Overview


ClosedXmlCrmWorkflow.png

Key design points

  • Visual Studio is used to build a CRM workflow activity plug-in (DLL).
  • ILMerge.exe is used to combine the workflow activity plug-in DLL with ClosedXML.dll and DocumentFormat.OpenXml.dll to produce a new .NET assembly. It is that assembly that you register with CRM using the Plug-in Registration Tool.
  • Once the merged assembly is registered, CRM workflow designers can set field (cell) values and define a formula for ClosedXML to evaluate. The results of the formula evaluation can be used to set CRM field values or can be the basis for additional logic.

Sample Usage: Calculate Hours Between Date/Time Entries

In this example, the custom workflow activity is used to calculate the number of hours between two DateTime field entries.

First, define the formula and the values to pass to the workflow activity.
WorkflowExcelCalc_Workflow2.png

The formula is (D1 - C1) * 24. This means, subtract the DateTime value in specified for Cell_C1_DateTime from the value for Cell_D1_DateTime and then multiply the results by 24.

Next, define the CRM field to update with the results of the formula calculation. In this case, we're updating a field named CalcHours (a "Double" data type).

WorkflowExcelCalc_Workflow3.png

Here's the summary of the two workflow process steps:
WorkflowExcelCalc_Workflow4.png

Note: The workflow process is registered on Create of an entity named zTemp. It is also registered on the Update event for the entity for fields Start Date and End Date.

To test the results, create or update a record. The workflow activity calculates the number of hours between the date/time entries and updates the record with the results.

WorkflowExcelCalc_Workflow5.png